import pygame
from pygame.sprite import Sprite

class Bucket(Sprite):
    def __init__(self, dg_settings, screen):
        # 初始化水桶并设置其初始位置
        super(Bucket, self).__init__()
        self.screen = screen
        self.dg_settings = dg_settings
        # 加载水桶图像并获取其外接矩形
        self.image = pygame.image.load('image/bucket.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        # 将水桶放置到屏幕的底部正中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        # 在水桶的属性center中存储小数值
        self.center = float(self.rect.centerx)
        # 移动标志
        self.moving_right = False
        self.moving_left = False

    # def update(self):
    #     # 根据移动标志调整水桶位置
    #     if self.moving_right :
    #         self.rect.centerx += self.dg_settings.bucket_speed_factor
    #     if self.moving_left :
    #         self.rect.centerx -= self.dg_settings.bucket_speed_factor
    #     # 根据self.center更新rect对象
    #     self.rect.centerx = self.center

    def update(self):
    # 如果不越界
        if self.moving_right==True and self.rect.right < 1000:
            self.rect.centerx=self.rect.centerx+self.dg_settings.bucket_speed_factor
        if self.moving_left==True and self.rect.left > 0:
            self.rect.centerx=self.rect.centerx-self.dg_settings.bucket_speed_factor

    def blitme(self):
        # 在指定位置绘制水桶
        self.screen.blit(self.image,self.rect)

